package cn.stylefeng.guns.modular.basic.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicArea;
import cn.stylefeng.guns.modular.basic.mapper.BasicAreaMapper;
import cn.stylefeng.guns.modular.basic.model.params.BasicAreaParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicCustomerParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicAreaResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicCustomerResult;
import  cn.stylefeng.guns.modular.basic.service.BasicAreaService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

/**
 * 库区表 服务实现类
 */
@Service
public class BasicAreaServiceImpl extends ServiceImpl<BasicAreaMapper, BasicArea> implements BasicAreaService {
    
    @Resource
    private DeptService deptService;

    @Override
    public void add(BasicAreaParam param){
        BasicArea entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BasicAreaParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BasicAreaParam param){
        BasicArea oldEntity = getOldEntity(param);
        BasicArea newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BasicAreaResult findBySpec(BasicAreaParam param,Long deptId,Long operateId){
        return this.baseMapper.findBySpec(param,deptId,operateId);
    }

    @Override
    public List<BasicAreaResult> findListBySpec(BasicAreaParam param,Long deptId,Long operateId){
        return this.baseMapper.findListBySpec(param,deptId,operateId);
    }

    @Override
    public LayuiPageInfo findPageBySpec(BasicAreaParam param,String condition,Long deptId,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition,deptId,operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BasicAreaParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BasicArea getOldEntity(BasicAreaParam param) {
        return this.getById(getKey(param));
    }

    private BasicArea getEntity(BasicAreaParam param) {
        BasicArea entity = new BasicArea();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public List<BasicArea> listByDeptId(Long deptId) {
		return this.baseMapper.listByDeptId(deptId);
	}

	@Override
	public List<BasicAreaResult> findAvailableList(Long deptId, Long deliverId,Long operateId) {
		BasicAreaParam param = new BasicAreaParam();
		param.setDeliverId(deliverId);
        return this.baseMapper.findListBySpec(param,deptId,operateId);
	}

}
